<?php
// +----------------------------------------------------------------------
// | Description: restFul风格 api接口基础类
// +----------------------------------------------------------------------
// | Author: dongpeng
// +----------------------------------------------------------------------

namespace app\common\controller;

class Rest extends ApiCommon
{
    public $modelName;
    public static $model;
    public function _initialize()
    {
        parent::_initialize();
        self::$model = model($this->modelName);
        self::$model->setParam($this->param);
    }

    public function index()
    {
        $data = self::$model->getDataList();
        $this->jsonData($data);
    }

    public function read()
    {
        $data = self::$model->getDataById($this->param['id']);
        $this->jsonData($data, self::$model->getError());
    }

    public function save()
    {
        $res = self::$model->createData($this->param);
        $this->checkRes($res, '添加成功', self::$model->getError());
    }

    public function update()
    {
        $res = self::$model->updateDataById($this->param, $this->param['id']);
        $this->checkRes($res, '编辑成功', self::$model->getError());
    }

    public function delete()
    {
        $res = self::$model->delDataById($this->param['id']);
        $this->checkRes($res, '删除成功', self::$model->getError());
    }

    public function deletes()
    {
        $res = self::$model->delDatas($this->param['ids']);
        $this->checkRes($res, '批量删除成功', self::$model->getError());
    }

    public function enables()
    {
        $res = self::$model->enableDatas($this->param['ids'], $this->param['status']);
        $this->checkRes($res, '批量操作成功', self::$model->getError());
    }
}
